<script>
  import {
    mapState,
    mapMutations
  } from 'vuex'
  export default {
    onLaunch: function() {
      // console.warn('当前组件仅支持 uni_modules 目录结构 ，请升级 HBuilderX 到 3.1.0 版本以上！')
      console.log('App Launch')

      // 用于在小程序启动后检查用户是否已经登录，如果没有登录则执行登录操作
      this.checkLogin(res => {
        console.log('is_login: ', res.is_login)
        if(res.is_login) {
          this.getUserInformation()
        }
        if (!res.is_login) {
          uni.showModal({
            title: '提示',
            content: '请先登录',
            success: (res) => {
              if (res.confirm) {
                uni.switchTab({
                  url: '/pages/my/my'
                });
              }
            }
          })
        }
      })


    },
    onShow: function() {
      console.log('App Show')
    },
    onHide: function() {
      console.log('App Hide')
    },



    methods: {
      ...mapMutations('m_user', ['updateUserInfo', 'updateUserClockInList', 'updateUserPlanList', 'updateUserRecordList', 'updateUserVideoList', 'updateUserArticleList', 'updateUserTrendList', 'updateUserLikeTrendList']),
      ...mapMutations('m_music', ['setUserMusicList']),
      ...mapMutations('m_cart', ['updateUserCartList']),
      // 判断token是否存在，如果token存在，则请求服务器，判断token是否有效
      checkLogin: function(callback) {
        var token = uni.getStorageSync('token')
        if (token) {
          uni.request({
            url: 'https://jeoz.top:3000/api/checkLogin',
            data: {
              token: token
            },
            success: res => {
              callback({
                is_login: res.data.is_login
              })
            }
          })
        } else {
          callback({
            is_login: false
          })
        }
      },
      async getUserInformation() {
        var token = uni.getStorageSync('token')
        console.log('tokentokentoken',token)
        // 调用获取用户所有信息资料
        const {
          data: res
        } = await uni.$http.get('/api/get/userInformation?token=' + token)
        console.log(res.message)
        this.updateUserInfo(res.message.userInfo)
        this.updateUserClockInList(res.message.clockinList)
        this.updateUserPlanList(res.message.planList)
        this.updateUserRecordList(res.message.recordList)
        this.updateUserVideoList(res.message.videoList)
        this.updateUserArticleList(res.message.articleList)
        this.updateUserCartList(res.message.cartList)
        this.updateUserTrendList(res.message.trendList)
        this.updateUserLikeTrendList(res.message.likeTrendList)
        this.setUserMusicList(res.message.musicList)
      }
    }
  }
</script>

<style lang="scss">
  /*每个页面公共css */
  @import '@/uni_modules/uni-scss/index.scss';
  /* #ifndef APP-NVUE */
  @import '@/static/customicons.css';

  // 设置整个项目的背景色
  page {
    background-color: #f5f5f5;
  }

  /* #endif */
  .example-info {
    font-size: 14px;
    color: #333;
    padding: 10px;
  }
</style>